<template>
  <view>
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar cancelButton="none" :radius="100" @input="inputHandler"></uni-search-bar>
    </view>
    
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="suggestionList.length > 0 ">
		  <view
		        class="sugg-item"
		        v-for="(item, index) in suggestionList"
		        :key="index"
		        @click="gotoProductDetail(item)"
		        >
		    <view class="goods-name">{{ item.goods_name }}</view>
		    <uni-icons type="arrowright" size="16"></uni-icons>
		  </view>
		</view>
		
		<!-- 搜索历史 -->
		<view v-else class="history-box">
		  
		  <!-- 标题区域 -->
		  <view class="history-title">
		    <text>搜索历史</text>
		    <uni-icons @click="removeHistoryList" type="trash" size="17"></uni-icons>
		  </view>
		  
		  <!-- 历史列表 -->
		  <view class="history-list">
		    <uni-tag @click="goProductList(item)" v-for="(item, index) in historyList" :key="index" :text="item"></uni-tag>
		  </view>
		  
		</view>
  </view>
</template>

<script>
export default {
	data() {
		return {
			// timer: null
			keyword: '', //关键词
			suggestionList: [], // 搜索列表
			 // 搜索关键词的历史记录
			historyList: uni.getStorageSync("historyList") ||  [],
		}
	},
  methods: {
    inputHandler(e) {
			// 防抖 -> 延时器
			// 先清除上一次的延时器
			if (this.timer) clearTimeout(this.timer)
			// 再开启
			this.timer = setTimeout(() => {
				console.log(e);
				// 1. 获取搜索的关键词
				// 2. 根据关键词 -> 获取搜索列表
				this.keyword = e
				this.getSuggestionList()
			}, 300)
    },
		goProductList(item) {
			uni.navigateTo({
			  url: `/pages_sub1/product-list/product-list?query=${item}`,
			});
		},
		gotoProductDetail(item) {
		  // 点击列表项，跳转到产品详情页
		  uni.navigateTo({
		    url: `/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`,
		  });
		},
		// 获取搜索列表
		async getSuggestionList() {
			if (!this.keyword) {
				this.suggestionList = []
				return 
			}
			const { meta: { status, msg }, message } = await this.$http.get('/goods/qsearch?query=' + this.keyword)
			if (status !== 200) return this.$msg(msg)
			this.suggestionList = message
			// 更新搜索历史
			this.updateHistoryList(this.keyword)
		},
		// 更新搜索历史列表
		updateHistoryList(keyword) {
			this.historyList.unshift(keyword)
			// 去重 set 
			// arr => set类型
			const arr = [...new Set(this.historyList)]
			if (arr.length > 4) {
				// 数组 开头  / 结尾 push pop
				arr.pop()
			}
			this.historyList = arr	
			// 持久化
			uni.setStorageSync("historyList", arr)
		},
		removeHistoryList() {
			this.historyList = []
			uni.removeStorageSync("historyList")
		}
  },
};
</script>

<style scoped lang="scss">
// 使用样式穿透：覆盖 uni-search-bar 组件的 .uni-searchbar 背景色
// scss ::v-deep
// less /deep/    
    
::v-deep .uni-searchbar {
  background-color: #c00000 !important;
}
// 搜索历史
.history-box {
  padding: 0 10rpx;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80rpx;
    font-size: 26rpx;
    border-bottom: 2rpx solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    uni-tag {
      margin-top: 10rpx;
      margin-right: 10rpx;
    }
  }
}
// 搜索框吸顶
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
// 搜索建议列表
.sugg-list {
  padding: 0 20rpx;

  .sugg-item {
    font-size: 24rpx;
    padding: 26rpx 0;
    border-bottom: 1rpx solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      margin-right: 6rpx;
      
      // 溢出部分隐藏
      overflow: hidden;
      
      // 文字不允许换行
      white-space: nowrap;
      
      // 文字溢出后使用省略号表示
      text-overflow: ellipsis;
    }
  }
}
</style>